/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundless.test;

import boundless.entities.*;
import boundless.managedbeans.*;
import boundless.ejb.*;
import java.util.ArrayList;
import java.util.List;

/**
 *  Tests not just the roster, but all of league and drafting as well.
 * @author hendrikdekock
 */
public class RosterTester {
    
    public static void start(RequestBean rb) {
        /*Create League and invite Users */
        int teamCount = 0;
        LeagueCreationBean lcb = new LeagueCreationBean();
        lcb.setReqBean(rb);
        
        System.out.println("GETTING TURKMENBASHI");
        Account turkmenbashi = rb.getUserByUsername("The Great Turkmenbashi");
        
        lcb.setCurUser(turkmenbashi);
        
        System.out.println("CREATING LEAGUE");
        lcb.createLeague("Turkmenbashi's Bouzkashi League", true);
        System.out.println("LEAGUE CREATED");
        LeagueBean lb = new LeagueBean();
        List<League> turkmenLeagues = rb.getLeaguesByOwner(turkmenbashi);
        for (League l : turkmenLeagues)
            System.out.println(l);
        League curLeague = turkmenLeagues.get(0);
        lb.setCurLeague(curLeague);
        lb.setReqBean(rb);
        
        System.out.println("Inviting accounts");
        lb.inviteByAccount(rb.getAccountByEmail("soda@stereo.com"));
        System.out.println("Let's invite this guy by email");
        lb.inviteByEmail("aha@aha.no");
 //       lb.inviteByAccount(rb.getUserByUsername("hdk"));
        System.out.println(rb.getUserByUsername("boundsoft"));
        lb.inviteByAccount(rb.getUserByUsername("boundsoft"));
        lb.inviteByAccount(rb.getAccountByEmail("tintin@herge.be"));
        System.out.println("Done inviting");
        AccountBean ab = new AccountBean();
        ab.setRequestBean(rb);
        ab.setAccount(rb.getUserByUsername("Vist DPRK!"));
        System.out.println("Trying to join League: " +ab.requestToJoinLeague(curLeague));
        
        
        lb.togglePermissions();
        
        System.out.println("Trying to join League: " + ab.requestToJoinLeague(curLeague));
        AccountBean ab2 = new AccountBean();
        ab2.setRequestBean(rb);
        ab2.setAccount(rb.getAccountByEmail("alan@alanparsonsproject.com"));
        ab2.requestToJoinLeague(curLeague);
        AccountBean ab3 = new AccountBean();
        ab3.setRequestBean(rb);
        ab3.setAccount(rb.getAccountById(10));
        ab3.requestToJoinLeague(curLeague);
        AccountBean ab4 = new AccountBean();
        ab4.setRequestBean(rb);
        ab4.setAccount(rb.getAccountById(79));
        ab4.requestToJoinLeague(curLeague);
        AccountBean ab5 = new AccountBean();
        ab5.setRequestBean(rb);
        ab5.setAccount(rb.getUserByUsername("musicaligera"));
        
        System.out.println("SODA STEREO WANTS TO JOIN: " +ab5.requestToJoinLeague(curLeague));
        System.out.println("Soda Stereo checks its invites");
        ab5.viewInvitations();
        for (Invitebyid i: ab5.getInvitations()) {
            System.out.println("Accepting invitation: " + i);
            ab5.acceptInvitation(i);
            teamCount++;
        }
        AccountBean ab6 = new AccountBean();
        ab6.setRequestBean(rb);
        ab6.setAccount(rb.getUserByUsername("The Faker"));
        ab6.requestToJoinLeague(curLeague);
        AccountBean ab7 = new AccountBean();
       ab7.setRequestBean(rb);
        ab7.setAccount(rb.getUserByUsername("boundsoft"));
        System.out.println("Boundsoft CHECKS HIS INVITATIONS");
        ab7.viewInvitations();
        for (Invitebyid i: ab7.getInvitations()) {
            System.out.println("DECLINING Invitation " + i);
            ab7.declineInvitation(i);
        }
        
        lb.inviteByAccount(rb.getAccountById(10));
        System.out.println("Checking join requests");
        lb.viewJoinRequests();
        for (Joinrequests jr : lb.getJoinRequests()) {
            System.out.println("VIEWING Join Request: " + jr);
            if (jr.getAccount().getId() == 2) {
                System.out.println("DECLINING request");
                lb.declineJoinRequest(jr);
            }
            else {
                System.out.println("ACCEPTING JOIN REQUEST");
                lb.acceptJoinRequest(jr);
                teamCount++;
            }
        }
        
        
        System.out.println("Currently joined: " +teamCount);
        
        AccountBean ab10 = new AccountBean();
        ab10.setRequestBean(rb);
        ab10.setAccount(rb.getAccountById(5));
        ab10.viewInvitations();
        for (Invitebyid i : ab10.getInvitations()) {
            System.out.println("VIEWING INVITATION " + i);
            ab10.acceptInvitation(i);
            teamCount++;
        }
        /*AccountBean ab11 = new AccountBean();
        ab11.setRequestBean(rb);
        ab11.setAccount(rb.getAccountById(423));
        ab11.viewInvitations();
        for (Invitebyid i : ab11.getInvitations()) {
            System.out.println("VIEWING INVITATION " + i);
            ab11.acceptInvitation(i);
            teamCount++;
        }*/
        
        AccountBean ab12 = new AccountBean();
        ab12.setRequestBean(rb);
        ab12.setAccount(rb.getAccountById(13));
        ab12.viewInvitations();
        for (Invitebyid i : ab12.getInvitations()) {
            System.out.println("VIEWING INVITATION " + i);
            ab12.acceptInvitation(i);
            teamCount++;
        }
        
        System.out.println("COUNT : " + teamCount);
        System.out.println("COUNT AS GIVEN: " + rb.getNumTeamsInLeague(curLeague));
        System.out.println("COUNT AS GIVEN ANOTHER WYA: " + curLeague.getAccountCollection().size());
        rb.synchronize();
        /*Let's make some teams*/
        String[] names = {"Denver Dyanmos", "London Lizards", "Pyongyang Pegasi", "Marlinspike Mastodons",
            "Ashgabat Ateliers", "Futbol de la Furia", "Oslo Orations", "New York Newts"
        };
        int i = 0;
        for (Account a : curLeague.getAccountCollection()) {
            TeamCreationBean tcb = new TeamCreationBean();
            tcb.setCurUser(a);
            tcb.setCurLeague(curLeague);
            tcb.setRequestBean(rb);
            
            System.out.println("Creating team: " + names[i] + ": " +tcb.createTeam(names[i]) 
                    + " For user " + a);
            i++;
        }
        System.out.println(rb.getNumTeamsInLeague(curLeague));
        rb.synchronize();
        
        //return;
        /*Now let's do our draft!*/
        System.out.println("STARTING DRAFT");
        DraftBean db = new DraftBean();
        db.setReqBean(rb);
        db.setLeague(curLeague);
        List<Player> quarterbacks = rb.getAllPlayersOfPosition(rb.getPosition(0));
        List<Player> runningbacks = rb.getAllPlayersOfPosition(rb.getPosition(1));
        List<Player> widereceivers = rb.getAllPlayersOfPosition(rb.getPosition(2));
        List<Player> tightends = rb.getAllPlayersOfPosition(rb.getPosition(3));
        List<Player> kickers = rb.getAllPlayersOfPosition(rb.getPosition(4));
        List<Player> defense = rb.getAllPlayersOfPosition(rb.getPosition(5));
        System.out.println("GOT ALL THE POSITIONS");
        
        //0 - 3 Runing back, 4 - 5 Wide receiver, 6 -7 tight end
        int flexPlayer = 0;
        
        System.out.println("STARTING THE DRAFT");
        List<Teamsbyleague> allTeams = rb.getAllTeamsInLeague(curLeague);
        System.out.println("DRAFTING " + allTeams.size() + " TEAMS");
        for (Teamsbyleague tl: allTeams) {
            Fantasyteams t = rb.getTeam(tl.getTeamid());
            //We're just going to make meaningless assignments by ginvg the right amount of each
            //All reserves will be 1QB + 2RB + 1TE + 2WR
            //Active Players:
            System.out.println("TEAM "  + t);
            ArrayList<Player> activePlayers = new ArrayList<Player>();
            ArrayList<Player> benchedPlayers = new ArrayList<Player>();
            //1 Quarterback
            int r = 0;
            Player curPlayer = quarterbacks.get(r);
            while (rb.isPlayerAssignedToLeague(curPlayer, curLeague)) {
                r++;
                curPlayer = quarterbacks.get(r);
            }
            System.out.println("Drafting player " + curPlayer);
            activePlayers.add(curPlayer);
            r++;
            curPlayer = quarterbacks.get(r);
            while (rb.isPlayerAssignedToLeague(curPlayer, curLeague)) {
                r++;
                curPlayer = quarterbacks.get(r);
            }
            System.out.println("Drafting for bench " + curPlayer);
            benchedPlayers.add(curPlayer);
            //2 Running Backs
            r = 0;
            curPlayer = runningbacks.get(r);
            while (rb.isPlayerAssignedToLeague(curPlayer, curLeague)) {
                r++;
                curPlayer = runningbacks.get(r);
            }
            System.out.println("Drafting player " + curPlayer);
            activePlayers.add(curPlayer);
            r++;
            curPlayer = runningbacks.get(r);
            while (rb.isPlayerAssignedToLeague(curPlayer, curLeague)) {
                r++;
                curPlayer = runningbacks.get(r);
            }
            System.out.println("Drafting player " + curPlayer);
            activePlayers.add(curPlayer);
            
            if (0 <= flexPlayer && flexPlayer < 4) {
                r++;
                curPlayer = runningbacks.get(r);
                while (rb.isPlayerAssignedToLeague(curPlayer, curLeague)) {
                    r++;
                    curPlayer = runningbacks.get(r);
                }
                System.out.println("Drafting player " + curPlayer);
                activePlayers.add(curPlayer);
            }
            
            r++;
            curPlayer = runningbacks.get(r);
            while (rb.isPlayerAssignedToLeague(curPlayer, curLeague)) {
                r++;
                curPlayer = runningbacks.get(r);
            }
            System.out.println("Drafting for bench " + curPlayer);
            benchedPlayers.add(curPlayer);
            
            r++;
            curPlayer = runningbacks.get(r);
            while (rb.isPlayerAssignedToLeague(curPlayer, curLeague)) {
                r++;
                curPlayer = runningbacks.get(r);
            }
            System.out.println("Drafting for bench " + curPlayer);
            benchedPlayers.add(curPlayer);
            
            //2 Wide  receievers
            r = 0;
            curPlayer = widereceivers.get(r);
            while (rb.isPlayerAssignedToLeague(curPlayer, curLeague)) {
                r++;
                curPlayer = widereceivers.get(r);
            }
            System.out.println("Drafting player " + curPlayer);
            activePlayers.add(curPlayer);
            r++;
            curPlayer = widereceivers.get(r);
            while (rb.isPlayerAssignedToLeague(curPlayer, curLeague)) {
                r++;
                curPlayer = widereceivers.get(r);
            }
            System.out.println("Drafting player " + curPlayer);
            activePlayers.add(curPlayer);
            
            if (flexPlayer == 4 || flexPlayer == 5) {
                r++;
                curPlayer = widereceivers.get(r);
                while (rb.isPlayerAssignedToLeague(curPlayer, curLeague)) {
                    r++;
                    curPlayer = widereceivers.get(r);
                }
                System.out.println("Drafting player " + curPlayer);
                activePlayers.add(curPlayer);
            }
            r++;
            curPlayer = widereceivers.get(r);
            while (rb.isPlayerAssignedToLeague(curPlayer, curLeague)) {
                r++;
                curPlayer = widereceivers.get(r);
            }
            System.out.println("Drafting for bench " + curPlayer);
            benchedPlayers.add(curPlayer);
            
            r++;
            curPlayer = widereceivers.get(r);
            while (rb.isPlayerAssignedToLeague(curPlayer, curLeague)) {
                r++;
                curPlayer = widereceivers.get(r);
            }
            System.out.println("Drafting for bench " + curPlayer);
            benchedPlayers.add(curPlayer);
            
            //1 Tight End
            
            r = 0;
            curPlayer = tightends.get(r);
            while (rb.isPlayerAssignedToLeague(curPlayer, curLeague)) {
                r++;
                curPlayer = tightends.get(r);
            }
            System.out.println("Drafting player " + curPlayer);
            activePlayers.add(curPlayer);
            
            if (flexPlayer == 6 || flexPlayer == 7) {
                r++;
                curPlayer = tightends.get(r);
                while (rb.isPlayerAssignedToLeague(curPlayer, curLeague)) {
                    r++;
                    curPlayer = tightends.get(r);
                }
                System.out.println("Drafting player " + curPlayer);
                activePlayers.add(curPlayer);
            }
            
            r++;
            curPlayer = tightends.get(r);
            while (rb.isPlayerAssignedToLeague(curPlayer, curLeague)) {
                r++;
                curPlayer = tightends.get(r);
            }
            System.out.println("Drafting for bench " + curPlayer);
            benchedPlayers.add(curPlayer);
            
            //1 Kicker
            r = 0;
            curPlayer = kickers.get(r);
            while (rb.isPlayerAssignedToLeague(curPlayer, curLeague)) {
                r++;
                curPlayer = kickers.get(r);
            }
            System.out.println("Drafting player " + curPlayer);
            activePlayers.add(curPlayer);
            
            //1 Defense
            
            r = 0;
            curPlayer = defense.get(r);
            while (rb.isPlayerAssignedToLeague(curPlayer, curLeague)) {
                r++;
                curPlayer = defense.get(r);
            }
            System.out.println("Drafting player " + curPlayer);
            activePlayers.add(curPlayer);
            
            
            //Now we have our team.  Will it go?
            System.out.println("Team ready to validate");
            boolean validTeam = db.validateTeam(activePlayers, benchedPlayers);
            System.out.println("Validated: " + validTeam);
            if(!validTeam) {
                throw new RuntimeException("BAD TEAM!!");
            }
            
            db.assignTeam(activePlayers, benchedPlayers, t);
        }
        db.createSchedule();
        System.out.println("ITS THIS MANY " + rb.getNumTeamsInLeague(curLeague));
    }
    
}